package com.sduept.nwld.dataserver.controller.library;

import com.sduept.bigdata.fault.enums.RegexTypeEnum;
import com.sduept.bigdata.fault.manager.StandardLibraryManager;
import com.sduept.bigdata.fault.parser.entity.EquipmentOverhaulETLLibrary;
import com.sduept.bigdata.fault.parser.entity.StandardLibrary;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.generator.ObjectId;
import com.sduept.nwld.dataserver.enums.OverhaulRegexTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.ArrayList;
import java.util.List;


/**
 * 
 * @author Author
 *
 */
@Named
@ViewScoped
public class StandardLibraryController extends AbstractController{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 9047973834870834347L;
	
	public  static final String LIB_PATH = "com.sduept.bigdata.fault.parser.entity.";
	private Class<? extends StandardLibrary> cls = null;

	@Autowired
	private StandardLibraryManager manager;
	
	private StandardLibrary current;
	private StandardLibrary selected;
	private String className;
	private List<StandardLibrary> records=new ArrayList<StandardLibrary>();
	private List<String> recordTypes=new ArrayList<String>();
	
	@SuppressWarnings("unchecked")
	@PostConstruct
	public void init(){
		className = getParameter("ClassName");
		if(StringUtils.isEmpty(className)){
			addErrorMessage("页面出错，请联系系统管理员！");
			return ;
		}
		try {
			String cname = LIB_PATH + className;
			cls = (Class<? extends StandardLibrary>) Class.forName(cname);
			records=manager.listAllStandardLibraries(cls);
		} catch (Exception e) {
			addErrorMessage("页面出错，请联系系统管理员！");
			e.printStackTrace();
		}
		initRecordTypes();
	}

	public void preCreate(){
		try {
			current = cls.newInstance();
			current.setRecordOrder(records.size()+1);
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
		}
	}
	
	public void preUpdate(){
		current=selected;
		current.setRecordType(getDescByCode(current.getRecordType()));
	}
	
	public void save(){
		if(StringUtils.isEmpty(current.getId())){
			current.setId(ObjectId.get().toString());
			records.add(current);
		}
		if(StringUtils.isNotEmpty(current.getRecordType())){
			String scode="";
			if(cls==EquipmentOverhaulETLLibrary.class){
				scode=OverhaulRegexTypeEnum.val(current.getRecordType()).getCode();
			}else{
				scode=RegexTypeEnum.val(current.getRecordType()).getCode();
			}
			current.setRecordType(scode);
		}
		manager.save(current);
		addSuccessMessage("保存成功！");
	}
	public String getDescByCode(String code){
		if(StringUtils.isEmpty(code)){
			return "";
		}
		if(cls==EquipmentOverhaulETLLibrary.class){
			OverhaulRegexTypeEnum typeEnum = OverhaulRegexTypeEnum.valBycode(code);
			if(null!=typeEnum){
				return typeEnum.getDesc();
			}
		}else{
			RegexTypeEnum typeEnum = RegexTypeEnum.valBycode(code);
			if(null!=typeEnum){
				return typeEnum.getDesc();
			}	
		}
		return "";
	}
	public void delete(){
		manager.deleteById(selected.getId(), cls);
		records.remove(selected);
		selected=null;
		addSuccessMessage("删除成功！");
	}
	
	public void  initRecordTypes() {
		recordTypes.clear();
		if(cls==EquipmentOverhaulETLLibrary.class){
			for(OverhaulRegexTypeEnum s:OverhaulRegexTypeEnum.values()){
				recordTypes.add(s.getDesc());
			}
		}else{
			for(RegexTypeEnum s:RegexTypeEnum.values()){
				recordTypes.add(s.getDesc());
			}
		}
		
	}
	public StandardLibrary getCurrent() {
		return current;
	}

	public void setCurrent(StandardLibrary current) {
		this.current = current;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public List<StandardLibrary> getRecords() {
		return records;
	}

	public void setRecords(List<StandardLibrary> records) {
		this.records = records;
	}

	public StandardLibrary getSelected() {
		return selected;
	}

	public void setSelected(StandardLibrary selected) {
		this.selected = selected;
	}

	public List<String> getRecordTypes() {
		return recordTypes;
	}

	public void setRecordTypes(List<String> recordTypes) {
		this.recordTypes = recordTypes;
	}

	
}
